package com.bwie.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.TbMenu;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.utils.Result;
import com.bwie.vo.LoginVo;
import com.bwie.vo.PageUserVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/index")
public class IndexController {
    @Autowired
    TbUserService tbUserService;

    @Autowired
    StringRedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result login(@RequestBody @Validated LoginVo loginVo){
        TbUser tbUser = tbUserService.getOne(new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, loginVo.getUserName()));
        if(tbUser==null){
            return Result.error().setMsg("用户不存在");
        }
        String hash = DigestUtil.md5Hex(loginVo.getUserPass());
        System.out.println("加密后的密码是"+hash);
        if(!hash.equals(tbUser.getUserPass())){
            return Result.error().setMsg("密码错误");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", tbUser.getUserId()).claim("userName", tbUser.getUserName()).claim("userPass", tbUser.getUserPass()).compact();
        String key = "token-" + tbUser.getUserId();
        redisTemplate.opsForValue().set(key,token,60, TimeUnit.MINUTES);
        tbUser.setUserPass(null);
        tbUser.setToken(token);
        return Result.ok().setData(tbUser);
    }
    @PostMapping("/getMenuTree")
    public Result getMenuTree(HttpServletRequest request){
        String token = request.getHeader("token");
        if(token==null){
            return Result.error().setMsg("请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer userId = body.get("userId", Integer.class);
        List<TbMenu> menuTree=tbUserService.getMenuTreeByUserId(userId);
        return Result.ok().setData(menuTree);
    }
    @PostMapping("/getUserList")
    public Result getUserList(@RequestBody PageUserVo pageUserVo){
        return tbUserService.getUserList(pageUserVo);
    }
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody TbUser tbUser){
        return tbUserService.updateUser(tbUser);
    }

    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestPart("file")MultipartFile file){
        return tbUserService.uploadFile(file);
    }
    @PostMapping("/deleteUserById/{userId}")
    public Result deleteUserById(@PathVariable("userId")Integer userId){
        return tbUserService.deleteUserById(userId);
    }
    @PostMapping("/deleteUserAll")
    public Result deleteUserAll(@RequestBody List<TbUser> userList){
        return tbUserService.deleteUserAll(userList);
    }
    @PostMapping("checkUser")
    public Result checkUser(@RequestBody TbUser tbUser){
        return tbUserService.checkUser(tbUser);
    }
}
